Skip to content

Conversation

@jcfr
Copy link
Contributor

@jcfr jcfr commented Sep 25, 2025

This PR completes the migration to Python 3 string APIs across PythonQt, updates the code generator and generated wrappers, and introduces an opt-in compatibility shim for legacy wrappers.

It removes reliance on PyString_* aliases in PythonQtPythonInclude.h for in-tree sources and makes Unicode usage explicit. The build now errors if Python 2.x is detected.

Summary:

  • Replace PyString_* with Python-3 equivalents:

    • PyString_FromStringPyUnicode_FromString
    • PyString_AS_STRINGPyUnicode_AsUTF8
    • PyString_AsStringPyUnicode_AsUTF8
    • PyString_FromFormatPyUnicode_FromFormat
    • PyString_CheckPyUnicode_Check
  • Update the generator to emit PyUnicode_*.

  • Update generated_cpp_* wrappers to use PyUnicode_*.

  • Add PYTHONQT_USE_PYSTRING_SHIM (default OFF) to alias PyString_FromStringPyUnicode_FromString for older, out-of-tree wrappers.

  • Enforce Python 3 at build time (hard error on Python 2.x).

@jcfr

This comment was marked as outdated.

@mrbean-bremen
Copy link
Contributor

My personal preference would be your second alternative. @usiems - what do you think?

@usiems
Copy link
Contributor

usiems commented Sep 25, 2025

Option 2 is good for me. We could also go with option 3, since we might call the next version 4.0 (mostly because we dropped Python 2 support), and this would justify some incompatibilities. But option 2 is certainly safer.

The generated wrappers only seem to use PyString_FromString, so, if we only care about existing generated wrapper code, it would be sufficient to keep just this one define.

By the way, what about the PyInt_ defines? Shouldn't they be replaced, too?

@jcfr
Copy link
Contributor Author

jcfr commented Sep 25, 2025

By the way, what about the PyInt_ defines? Shouldn't they be replaced, too?

@jcfr
Copy link
Contributor Author

jcfr commented Sep 25, 2025

Build / oldschool (rockylinux, 9, debug) (pull_request)

Reported error1 is unrelated to the proposed changes:

Complete!
Last metadata expiration check: 0:00:55 ago on Thu Sep 25 20:13:12 2025.
Package which-2.21-30.el9_6.x86_64 is already installed.
Error: 
 Problem: package qt5-qtbase-private-devel-5.15.9-11.el9_6.x86_64 from appstream requires cups-devel, but none of the providers can be installed
  - cannot install the best candidate for the job
  - nothing provides cups-libs(x86-32) = 1:2.3.3op2-33.el9_6.1 needed by cups-devel-1:2.3.3op2-33.el9_6.1.i686 from appstream
  - nothing provides cups-libs(x86-64) = 1:2.3.3op2-33.el9_6.1 needed by cups-devel-1:2.3.3op2-33.el9_6.1.x86_64 from appstream
(try to add '--skip-broken' to skip uninstallable packages or '--nobest' to use not only best candidate packages)

Footnotes

  1. https://github.com/MeVisLab/pythonqt/actions/runs/18019484218/job/51273010918?pr=308

@jcfr jcfr changed the title chore(PythonQt): Migrate string API usage to Python 3 PyUnicode_* chore(PythonQt): Migrate to Python-3 Unicode API; update generator/wrappers; add opt-in PyString shim; enforce Python ≥ 3 Sep 25, 2025
@@ -50,7 +50,7 @@ PythonQtShell_QAbstractAnimation::~PythonQtShell_QAbstractAnimation() {
void PythonQtShell_QAbstractAnimation::childEvent(QChildEvent* arg__1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should remove these older versions of generated code now, instead of adapting them to these changes each time.
Qt versions before 5.9 are not tested at all, and we do not test the checked-in generated code anyway, so we cannot guarantee that it will still work with the current version of PythonQt. Had also a discussion with @usiems about this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would tend to remove all checked-in versions except perhaps the 5.15 one. 5.15 can serve as an example of what kind of code is generated, but I would recommend to anyone using that Qt version to re-run the generator even so.

#define PY3K
// Helper defines to facilitate porting
#define PyString_FromString PyUnicode_FromString
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a deprecation warning here? Or would this over-flood the log with warnings?

@@ -50,7 +50,7 @@ PythonQtShell_QAbstractAnimation::~PythonQtShell_QAbstractAnimation() {
void PythonQtShell_QAbstractAnimation::childEvent(QChildEvent* arg__1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would tend to remove all checked-in versions except perhaps the 5.15 one. 5.15 can serve as an example of what kind of code is generated, but I would recommend to anyone using that Qt version to re-run the generator even so.

…ring`

Make Unicode creation explicit instead of relying on the compatibility
macros in `PythonQtPythonInclude.h`. No functional change intended.
This removes dependence on the `PyString_*` shim and documents the Python 3
assumption clearly. No functional change intended.
Port remaining conversions to the Unicode API and drop reliance on the macro
alias in `PythonQtPythonInclude.h`. No functional change intended.
…rmat`

Format string creation now uses the Unicode API directly.
No functional change intended.
Update assertions and type checks to the Unicode API.
No functional change intended.
…tring`

Make Unicode creation explicit instead of relying on the compatibility
macros in `PythonQtPythonInclude.h`. No functional change intended.
…_FromString`

Make Unicode creation explicit instead of relying on the compatibility
macros in `PythonQtPythonInclude.h`. No functional change intended.
@jcfr jcfr force-pushed the update-python3-string-handling branch from ed2750d to e793d1a Compare September 26, 2025 19:46
@mrbean-bremen mrbean-bremen merged commit 5a891f2 into MeVisLab:master Sep 26, 2025
17 checks passed
@jcfr jcfr deleted the update-python3-string-handling branch September 26, 2025 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants